//
//  BCRefreshAutoFooter.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/12.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCRefreshAutoFooter.h"

@interface BCRefreshAutoFooter ()

/**
 是否需要底部安全距离
 */
@property (nonatomic, assign) BOOL needBottomSafeArea;

/**
 *IndicatorView
 */
@property (nonatomic, weak) UIActivityIndicatorView *indicatorView;

@end

@implementation BCRefreshAutoFooter

+ (instancetype)footerWithRefreshingTarget:(id)target refreshingAction:(SEL)action needBottomSafeArea:(BOOL)need {
    BCRefreshAutoFooter *footer = [super footerWithRefreshingTarget:target refreshingAction:action];
    footer.needBottomSafeArea = need;
    return footer;
}

- (void)prepare {
    [super prepare];
    //
    //
    CGFloat height = MJRefreshFooterHeight+Height_Bottom_SafeArea;
    self.mj_h = height;
    self.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    self.onlyRefreshPerDrag = true;
    self.refreshingTitleHidden = true;
    //
    [self setTitle:@"" forState:MJRefreshStateIdle];
    [self setTitle:@"" forState:MJRefreshStatePulling];
    [self setTitle:@"" forState:MJRefreshStateWillRefresh];
    [self setTitle:@"" forState:MJRefreshStateRefreshing];
    //
}
- (void)placeSubviews {
    [super placeSubviews];
    //得到UIActivityIndicatorView
    if (!self.indicatorView) {
        for (UIView *subview in self.subviews) {
            if ([subview isKindOfClass:UIActivityIndicatorView.class]) {
                self.indicatorView = (UIActivityIndicatorView *)subview;
                break;
            }
        }
    }
    //
    if (self.needBottomSafeArea) {//需要重新布局
        self.stateLabel.mj_h = MJRefreshFooterHeight;
        //
        if (self.indicatorView) {
            CGPoint center = self.indicatorView.center;
            //
            self.indicatorView.center = CGPointMake(center.x, MJRefreshFooterHeight/2);
        }
    }
}

@end
